--#include "data\config\misc\ReincarnationConfig.lua" once	-轮回消耗配置
--#include "data\functions\Activity\Common\Consumes.lua" --消耗 通用
--#include "data\functions\Consume.lua" once

function CheckReincarnationSoulExchange(sysarg,config)
	--local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	if not Actor.checkActorLevel(sysarg, config.reqLevel, 0) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.Reincarnation001, config.reqLevel), ttFlyTip)
		return false
	end
	--[[
	local var = Actor.getStaticVar(sysarg)	
	if not var.days then var.days = {} end
	local leftReincarnation = var.days.leftReincarnation
	if leftReincarnation ~= nil and leftReincarnation <= 0 then
		-- 当天的轮回兑换次数已经用完
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Reincarnation006, ttFlyTip)
		return false
	end
	]]

	if Consumes.Check(sysarg, config.consumes) ~= true then 
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.coin00001, ttFlyTip)
		return false
	end
	return true
end

MiscsImplement.ReincarnationExCountHandle = function(sysarg,msgid,packet)
	SendActorLeftReincarnation(sysarg, nil)
end

function SendActorLeftReincarnation(sysarg, leftReincarnation)
	--[[
	if leftReincarnation == nil then
		local var = Actor.getStaticVar(sysarg)
		if not var.days then var.days = {} end
		leftReincarnation = var.days.leftReincarnation
		if leftReincarnation == nil then
			leftReincarnation = ReincarnationCfg.dailyCountLimit -- 默认是3次
		end
	end
	
	-- 下发今天轮回兑换修为剩余次数
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sExchangeReincarnationCount)
	DataPack.writeByte(netPack, leftReincarnation)	
	DataPack.flush(netPack)
	]]
end

function HandleExchangeReincarnationSoul(sysarg)
	local actorLevel = Actor.getIntProperty(sysarg,PROP_CREATURE_LEVEL)
	local config = ReincarnationCfg.ReincarnationSoulExchange[actorLevel - ReincarnationCfg.activeLevel]
	if not config then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.Reincarnation005, ReincarnationCfg.activeLevel), ttFlyTip)
		System.trace("Can not find ReincarnationSoulExchange config,actorLevel="..actorLevel)
		return
	end

	local bCheck = CheckReincarnationSoulExchange(sysarg,config)
	if bCheck ~= true then return  end 
	local  bResult
	
	bResult = Consumes.Remove(sysarg,config.consumes, 1201,Lang.LogTips.ReincarnationExLog)	
	if bResult ~= true then return end

	--[[
	local var = Actor.getStaticVar(sysarg)
	if not var.days then var.days = {} end
	local leftReincarnation = var.days.leftReincarnation
	if leftReincarnation == nil then -- 今天剩余的转数(默认是3次)
		leftReincarnation = ReincarnationCfg.dailyCountLimit - 1			
	else
		leftReincarnation = leftReincarnation - 1
	end
	var.days.leftReincarnation = leftReincarnation
	]]

	local currentSoul = Actor.getIntProperty(sysarg,PROP_ACTOR_REINCARNATION_VALUE)
	Actor.setIntProperty(sysarg,PROP_ACTOR_REINCARNATION_VALUE,currentSoul + config.addSoul)
	Actor.setLevel(sysarg,config.chgLevel)
	Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.c000196, config.addSoul, currentSoul + config.addSoul), ttFlyTip)
	
	-- 下发今天剩余转生次数(已经改为今天兑换修为剩余次数)
	--SendActorLeftReincarnation(sysarg, leftReincarnation)
	
	return 
end

MiscsImplement.ExchangeReincarnationSoulHandle = function(sysarg,msgid,packet)
	HandleExchangeReincarnationSoul(sysarg)
end



--轮回处理
MiscsImplement.ReincarnationHandle = function( sysarg, msgid, packet )
	local var = Actor.getStaticVar(sysarg)

	local Reinlevel = Actor.getIntProperty(sysarg, PROP_ACTOR_REINCARNATION)
	
	ReincarnationHandleImpl(sysarg, Reinlevel + 1)
		
end


--保存曾经到达过的最高等级
function AddOnceLevelsRein( sysarg , nCurrentLevel)
	local var = Actor.getStaticVar( sysarg )
	if not var.OnceLevel then
		var.OnceLevel=0
	end
	if nCurrentLevel>var.OnceLevel then
	var.OnceLevel = nCurrentLevel
	end
end

function ReincarnationHandleImpl(sysarg, nReincarnation)
	local config = ReincarnationCfg.ReincarnationConsumes[nReincarnation]
	if not config then
		System.trace("Can not find ReincarnationConsumes config, reincarnation="..nReincarnation)
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.Reincarnation004,ttFlyTip)
		return
	end
	
	--条件检测
	local bCheck = CheckReincarnationCondition(sysarg, config)
	if bCheck ~= true then
		return
	end
	
	local currentSoul = Actor.getIntProperty(sysarg,PROP_ACTOR_REINCARNATION_VALUE)
	--local currentLevel = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	if not Actor.checkActorLevel(sysarg, ReincarnationCfg.activeLevel, 0) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.COM0055, ReincarnationCfg.activeLevel),ttFlyTip)
		return
	end

	if not DoConsumeCond( sysarg, config.consumes, 1, 1200, Lang.LogTips.ReincarnationLog ) then
		return
	end
	
	--轮回
	--Actor.SetActorReincarnation(sysarg, nReincarnation)
	
	local level =  math.floor((nReincarnation - 1)/ReincarnationCfg.stepNum)
	local step = nReincarnation - level * ReincarnationCfg.stepNum
	local msg = string.format(Lang.ScriptTips.Reincarnation003, level, step)
	Actor.sendTipmsg(sysarg, msg, ttFlyTip)
end

--轮回条件检测
function CheckReincarnationCondition(sysarg, config)

	--local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	if config.reqLevel and not Actor.checkActorLevel(sysarg, config.reqLevel, 0) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.Reincarnation001, config.reqLevel), ttFlyTip)
		return false
	end	

	if not CheckConsumeCond( sysarg, config.consumes, 1 ) then
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.Reincarnation002, ttFlyTip)
		return false
	end
	
	return true
end


MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cGetReincarnation, MiscsImplement.ReincarnationHandle)
MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cReincarnationExchange, MiscsImplement.ExchangeReincarnationSoulHandle)
MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cGetReincarnationExCount, MiscsImplement.ReincarnationExCountHandle)